package main

import (
	"log"

	"github.com/streadway/amqp"
)

var dimap = make(map[string]string)

func main() {
	dimap["1"] = "error"
	dimap["2"] = "debug"
	conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
	failOnError(err, "Failed to connect to RabbitMQ")
	defer conn.Close()

	ch, err := conn.Channel()
	failOnError(err, "Failed to open a channel")
	defer ch.Close()

	err = ch.ExchangeDeclare("logs_topic", amqp.ExchangeTopic, true, false, false, false, nil)
	failOnError(err, "Failed to declare an exchange")

	q, err := ch.QueueDeclare("", false, false, false, false, nil)
	failOnError(err, "Failed to declare a queue")

	for _, key := range dimap {
		// 	log.Printf("Binding queue %s to exchange %s with routing key %s",
		// 		q.Name, "logs_topic", key)
		err = ch.QueueBind(q.Name, key, "logs_topic", false, nil)
		// 	failOnError(err, "Failed to bind a queue")
	}

	msgs, err := ch.Consume(q.Name, "", true, false, false, false, nil)
	failOnError(err, "Failed to register a consumer")

	go func() {
		for msg := range msgs {
			log.Printf("Received a message: %s", msg.Body)
		}
	}()

	<-make(chan bool)
}

func failOnError(err error, msg string) {
	if err != nil {
		log.Fatalf("%s: %s", msg, err)
	}
}
